package com.cs.excelreadimport.easyexcel.constants;

import lombok.Data;

import java.util.List;

@Data
public class Page<T> {
    private int currentPage; // 当前页码
    private int pageSize; // 每页显示的记录数
    private long totalRecords; // 总记录数
    private List<T> records; // 当前页的记录

    public Page() {
    }

    public Page(int currentPage, int pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }

    public Page(int currentPage, int pageSize, long totalRecords, List<T> records) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
        this.totalRecords = totalRecords;
        this.records = records;
    }


    public int getTotalPages() {
        return (int) Math.ceil((double) totalRecords / pageSize);
    }

    public boolean isFirstPage() {
        return currentPage == 1;
    }

    public boolean isLastPage() {
        return currentPage == getTotalPages();
    }

    public int getNextPage() {
        if (isLastPage()) {
            return currentPage;
        }
        return currentPage + 1;
    }

    public int getPreviousPage() {
        if (isFirstPage()) {
            return currentPage;
        }
        return currentPage - 1;
    }

    public int getOffset() {
        return (currentPage - 1) * pageSize;
    }

    @Override
    public String toString() {
        return "Page{" +
                "currentPage=" + currentPage +
                ", pageSize=" + pageSize +
                ", totalRecords=" + totalRecords +
                ", records=" + records +
                '}';
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(long totalRecords) {
        this.totalRecords = totalRecords;
    }

    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }
}
